import { createRouter, createWebHistory } from "vue-router";
import store from '../store'; // 直接导入 Vuex store

import Index from "@/views/index/index.vue";
import Login from "@/views/login/index.vue";
import Reset from "@/views/reset/index.vue";
import Jump from "@/views/index/jump.vue";

// 发布需求三个页面
// import IssueCloud from "@/views/index/issue/IssueCloud.vue";
// import IssueSite from "@/views/index/issue/IssueSite.vue";
// import IssuePack from "@/views/index/issue/IssuePack.vue";

const routes = [
	{
		path: "/",
		name: "Index",
		component: Index,
		meta: {
			pageTitle: '请登录', // 页面标题
		}
	},
	{
		path: "/login",
		name: "Login",
		component: Login,
		meta: {
			pageTitle: '请登录', // 页面标题
		}
	},
	{
		path: "/reset",
		name: "Reset",
		component: Reset
	},
	{
		path: "/index/jump.vue",
		name: "Jump",
		component: Jump
	},
	// {
	// 	path: "/index/issue/IssueCloud.vue",
	// 	name: "IssueCloud",
	// 	component: IssueCloud
	// },
	// {
	// 	path: "/index/issue/IssueSite.vue",
	// 	name: "IssueSite",
	// 	component: IssueSite
	// },
	// {
	// 	path: "/index/issue/IssuePack.vue",
	// 	name: "IssuePack",
	// 	component: IssuePack
	// }
];
const router = createRouter({
	history: createWebHistory(),
	routes
});
router.beforeEach((to, from, next) => {
	// 检查目标路由是否需要认证,认证有没有权限访问路由
	const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
	const isExist = to.matched.some(record => record.path === to.path)

	if (!requiresAuth) {
		if (isExist) {
			if (store.state.token) {
				console.log('有token走到这一步', store.state.token);
				next();
			} else {
				console.log( '没有token走到这一步', store.state.token);
				next();
			}
		} else {
			// 地址栏输入的路由地址没有找到，则返回首页
			next('/');
		}
	}
})

export default router;
